/* eslint-disable */
import qs from 'qs';
import md5 from 'js-md5';

function alphabeticalSort(a, b) {
  try {
    return a.localeCompare(b);
  } catch (e) {
    return a > b;
  }
}

function getSession(name) {
  const data = sessionStorage.getItem(name);
  try {
    return JSON.parse(data);
  } catch (e) {
    return data;
  }
}

class EncryptSign {
  constructor() {
    this.keyName = [
      [115, 105, 103, 110, 97, 116, 117, 114, 101],
      [110, 111, 110, 99, 101, 115, 116, 114],
      [97, 112, 112, 73, 100],
      [116, 105, 109, 101, 115, 116, 97, 109, 112],
    ];
  }
  getKeyName(idx) {
    let ret = '';
    for (let i = 0; i < this.keyName[idx].length; i++) {
      ret += String.fromCharCode(this.keyName[idx][i]);
    }
    return ret;
  }
  sign(object) {
    object = { ...object };

    object[this.getKeyName(1)] = Math.random()
      .toString(36)
      .substr(2, 15);
    object[this.getKeyName(3)] = Date.now() + +getSession('maiya_admin');
    let string = qs.stringify(object, {
      allowDots: true,
      arrayFormat: 'repeat',
      sort: alphabeticalSort,
      encode: true,
    });
    object[this.getKeyName(2)] = 'maiya-admin';
    string += md5(object[this.getKeyName(2)]).substr(7, 16);

    object[this.getKeyName(0)] = md5(string);
    return object;
  }
}

export default new EncryptSign();
